package com.github.xuemingyu.springboot.acticitycloud.controller.api;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.vo.Page;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.github.xuemingyu.springboot.acticitycloud.driver.dingtalk.DingTalkService;
import com.github.xuemingyu.springboot.acticitycloud.entity.SakuraEntryForm;
import com.github.xuemingyu.springboot.acticitycloud.service.SakuraEntryFormService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/1/28
 * \* Time: 11:26
 * \
 */
@RestController
@RequestMapping("/sakuraEntryForm")
public class SakuraEntryFormApiController {
    @Autowired
    private SakuraEntryFormService sakuraEntryFormService;
    @Autowired
    private DingTalkService dingTalkService;

    /**
     * 获取报名列表
     *
     * @param sakuraEntryForm
     * @param page
     * @return
     */
    @RequestMapping("/query")
    public ApiResult query(SakuraEntryForm sakuraEntryForm, Page<SakuraEntryForm> page) {
        sakuraEntryFormService.queryLike(sakuraEntryForm, page);
        return ApiResult.success(page);
    }

    /**
     * 报名
     *
     * @param sakuraEntryForm
     * @return
     */
    @RequestMapping("/add")
    public ApiResult add(SakuraEntryForm sakuraEntryForm, HttpServletRequest request) {
        sakuraEntryForm.setUserId(request.getHeader("userId"));
        try {
            sakuraEntryFormService.insert(sakuraEntryForm);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("报名成功");
    }

    /**
     * 取消报名
     *
     * @param id
     * @return
     */
    @RequestMapping("/cancel")
    public ApiResult cancel(Long id, HttpServletRequest request) {
        try {
            String userId = request.getHeader("userId");
            sakuraEntryFormService.cancel(id, userId);
        } catch (Exception e) {
            return ApiResult.error(e.getMessage());
        }
        return ApiResult.success("已取消");
    }

    /**
     * 修改报名信息
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/update")
    public ApiResult update(SakuraEntryForm sakuraEntryForm, HttpServletRequest request) {
        sakuraEntryForm.setUserId(request.getHeader("userId"));
        sakuraEntryFormService.update(sakuraEntryForm);
        return ApiResult.success("修改成功");
    }

    /**
     * 查询报名信息
     *
     * @param activityId
     * @param request
     * @return
     */
    @RequestMapping("/findOne")
    public ApiResult findOne(Long activityId, HttpServletRequest request) {
        SakuraEntryForm userId = sakuraEntryFormService.findOne(activityId, request.getHeader("userId"));
        return ApiResult.success(userId);
    }

    @RequestMapping("/getUserInfo")
    public Map<String, Object> getUserInfo(HttpServletRequest request) throws ApiException {
        OapiUserGetResponse user = dingTalkService.getUserInfoByUserId(request.getHeader("userId"));
        Map<String, Object> map = new HashMap<>();
        map.put("name", user.getName());
        map.put("phone", user.getMobile());
        return map;
    }
}
